﻿using UnityEngine;
using System.Threading;
using System;
using System.Collections;

public class Personagem : MonoBehaviour
{

		private GameObject mesa;
		private bool sairDoBar = false;
		private Vector3 posicaoInicial;
		public float velocidade = 3f;
		public int tempoEsperaAtendimento = 10;



		// Use this for initialization
		void Start ()
		{

				this.mesa = GameObject.Find ("triggerLugarMesa");
				posicaoInicial = this.transform.position;


		}

		// Update is called once per frame
		void Update ()
		{
				if (!sairDoBar) {
						mover ();
				} else {


						sair ();
						if (transform.position == posicaoInicial) {
								sairDoBar = false;
						}

				}

		}

		void mover ()
		{
				transform.position = Vector3.MoveTowards (transform.position, mesa.transform.position, velocidade);
		}

		void sair ()
		{

				transform.position = Vector3.MoveTowards (transform.position, posicaoInicial, velocidade);


		}

		void OnTriggerEnter (Collider c)
		{
				Debug.Log ("Entrou no OnTrigger");

				Debug.Log (c.isTrigger);
				Debug.Log (c.gameObject.name);
				if (c.gameObject == mesa) {
						Debug.Log ("Entrou no if");
						renderer.enabled = false;
						StartCoroutine (esperaAtendimento ());


				}

		}

		IEnumerator esperaAtendimento ()
		{
				yield return new WaitForSeconds (tempoEsperaAtendimento);
				renderer.enabled = true;
				sairDoBar = true;
				yield break;
		}
}



